package com.kpmg.datalake.common.vo;

import com.kpmg.datalake.common.enums.CacheTypeEnum;
import com.kpmg.datalake.common.enums.ResultCodeEnum;
import com.kpmg.datalake.common.service.IComLangMpngService;
import com.kpmg.datalake.common.utils.SpringUtil;
import java.io.Serializable;
import java.text.MessageFormat;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Echo create 2018-05-17 desc 高复用的统一结果返回json对象
 */
public class ServerResponse<T extends Serializable> implements Serializable {

  private static final long serialVersionUID = 1L;

  private static IComLangMpngService comLangMpngService = null;

  private int status;
  private String msg;
  private T data;
  private Object[] params;

  private ServerResponse(int status) {
    this(status, null);
  }

  private ServerResponse(int status, String msg, T data) {
    this(status, msg, data, null);
  }

  private ServerResponse(int status, String msg, T data, Object[] params) {
    this.status = status;
    this.msg = msg;
    this.data = data;
    this.params = params;
  }

  private ServerResponse(int status, String msg) {
    this(status, msg, null);
  }

  /**
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> serverResponse(ResultCodeEnum resultCode,
      Object[] params, T data) {
    return new ServerResponse<>(resultCode.getCode(), resultCode.getDisplay(), data, params);
  }

  /**
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> createBySuccess() {
    return new ServerResponse<>(ResultCodeEnum.CODE0000.getCode());
  }

  /**
   * @param msg 信息
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> createBySuccessMessage(String msg) {
    return createBySuccess(msg, null);
  }

  /**
   * @param data 信息
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> createBySuccess(T data) {
    return createBySuccess(null, data);
  }

  /**
   * @param msg 信息
   * @param data 信息
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> createBySuccess(String msg, T data) {
    return new ServerResponse<>(ResultCodeEnum.CODE0000.getCode(), msg, data);
  }

  /**
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> createByError() {
    return createByErrorCodeMessage(ResultCodeEnum.CODE9999.getCode(),
        ResultCodeEnum.CODE9999.getDisplay());
  }

  /**
   * @param errorMessage 信息
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> createByErrorMessage(
      String errorMessage) {
    return createByErrorCodeMessage(ResultCodeEnum.CODE9999.getCode(), errorMessage);
  }

  /**
   * @param errorMessage 信息
   * @param <T> 类型
   * @return ServerResponse
   */
  public static <T extends Serializable> ServerResponse<T> createByErrorCodeMessage(int errorCode,
      String errorMessage) {
    return new ServerResponse<>(errorCode, errorMessage);
  }

  public static <T extends Serializable> ServerResponse<T> createByErrorCodeMessage(
      ResultCodeEnum resultCodeEnum, String errorMessage) {
    return createByErrorCodeMessage(resultCodeEnum.getCode(), errorMessage);
  }

  /**
   * 使之不在json序列化结果当中
   *
   * @return boolean
   */
  public boolean isSuccess() {
    return this.status == ResultCodeEnum.CODE0000.getCode();
  }

  public int getStatus() {
    return status;
  }

  public T getData() {
    return data;
  }

  public String getMsg() {
    if (comLangMpngService == null) {
      comLangMpngService = SpringUtil.getBean(IComLangMpngService.class);
    }
    //获取国际化信息
    String returnMsg = comLangMpngService
        .getMultilingualByCode(CacheTypeEnum.MULTILINGUAL.getValue() + ":" + status);
    returnMsg = StringUtils.isBlank(returnMsg) ? "information not configured：" + msg : returnMsg;
    //是否属于参数占位符替换
    if (this.params != null) {
      MessageFormat messageFormat = new MessageFormat(returnMsg);
      returnMsg = messageFormat.format(this.params);
    }
    return returnMsg;
  }

  public String getStack() {
    return msg;
  }

}

